<style scoped>
.img-face {
  max-width: 200px;
  max-height: 200px;
  padding: 10px;
  cursor: pointer;
}
</style>
<template>
  <div>
    <Card style="margin-bottom: 10px">
      <Form inline class="form-search">
        <FormItem>
          <DatePicker style="width: 150px" type="date" format="yyyy年MM月dd日" placeholder="开始日期" :clearable="false"
            v-model="sdate"></DatePicker>
        </FormItem>
        <FormItem>
          <DatePicker style="width: 150px" type="date" format="yyyy年MM月dd日" placeholder="结束日期" :clearable="false"
            v-model="edate"></DatePicker>
        </FormItem>
        <FormItem> </FormItem>
        <FormItem>
          <Input v-model="keyword" placeholder="客户名称/商品" style="width: 200px" />
        </FormItem>

        <FormItem>
          <Button type="primary" @click="
  page = 1;
SearchSaleOrderDetail();
          " icon="md-search">查询</Button>
        </FormItem>
        <FormItem>
          <Button type="primary" @click="ExportData()" icon="md-download">导出</Button>
        </FormItem>
      </Form>
    </Card>
    <Table ref="table" style="margin-bottom: 10px" border   show-summary :summary-method="handleSummary"
      :loading="loadingTable" :columns="tableTitle" :data="tableData" :height="tableHeight"></Table>
    <Page :total="total" :current="page" :page-size="size" :page-size-opts="[10, 50, 100, 300, 500, 1000, 2000, 5000]"
      @on-change="
        (s) => {
          page = s;
          SearchSaleOrderDetail();
        }
      " @on-page-size-change="
  (p) => {
    size = p;
    page = 1;
    SearchSaleOrderDetail();
  }
" show-total show-sizer /> 
  </div>
</template>

<script>
import {
  SearchSaleOrderDetail, 
} from '@/api/saleorder' 
import excel from "@/libs/excel";
export default {
  name: 'saleorderdetail', 
  data () {
    return {
      Extras: {}, 
      total: 0,
      page: 1,
      size: 50,
      tableHeight: 450,
      sdate: '',
      edate: '',
      keyword: '',
      ruleValidate: {},
      loading: false,
      loadingTable: false,
      tempDataId: 0,
      tempType: 0,
      isPrint: 0,
      modalData: false,
      tableTitle: [
        {
          type: 'index',
          key: 'index',
          title: '序号',
          align: 'center',
          width: 100,
          render: (h, params) => {
            return h('span', params.index + (this.page - 1) * this.size + 1)
          }
        },
        
        {
          key: 'OrderDateStr',
          title: '单据日期',
          width: 150,
          align: 'center',
          
        },
         
        {
          key: 'ProductName',
          title: '商品名称',
          width: 250,
          align: 'center',
         
        },
        {
          key: 'Price',
          title: '单价',
          width: 100,
          align: 'center',
         
        },
        {
          key: 'Num',
          title: '采购数量',
          width: 100,
          align: 'center',
         
        },
        {
          key: 'Unit',
          title: '单位',
          width: 100,
          align: 'center',
         
        },
       
        {
          key: 'Amount',
          title: '金额',
          width: 100,
          align: 'center',
         
        },
        {
          key: 'CustomerName',
          title: '客户',
          width: 150,
          align: 'center'
        },
 
        
        {
          key: 'CreatedUserName',
          title: '销售人',
          width: 100,
          align: 'center'
        },
        
        {
          key: 'Remark',
          title: '备注',
          minWidth: 120,
          align: 'center'
        },
        
      ],
      tableData: [],
      allRoles: [],
      tempRoleIds: [],
      checkAll: false,
      tempDetail: [],
      modalChose: false,
      configData: {},
      dataProduct: []
    }
  },
  computed: {
    
  },
  methods: {
     
    handleSummary ({ columns, data }) {
      let vm = this
      const sums = {}
      columns.forEach((column, index) => {
        const key = column.key
        if (index === 0) {
          sums[key] = {
            key,
            value: '合计'
          }
        } else if (key == 'Amount') {
          sums[key] = {
            key,
            value: Number(vm.Extras.Amount.toFixed(2))
          }
        } else if (key == 'Num') {
          sums[key] = {
            key,
            value: Number(vm.Extras.Num)
          }
        }   else {
          sums[key] = {
            key,
            value: ' '
          }
        }
      })
      return sums
    },
    SearchSaleOrderDetail() {
      let vm = this
      vm.loadingTable = true
      if (typeof vm.sdate === 'object') {
        vm.sdate = vm.dayjs(vm.sdate).format('YYYY年MM月DD日')
      }
      if (typeof vm.edate === 'object') {
        vm.edate = vm.dayjs(vm.edate).format('YYYY年MM月DD日')
      }
      var reqData = {
        page: vm.page,
        size: vm.size,
        sdate: vm.sdate,
        edate: vm.edate,
        keyword: vm.keyword
      }
      SearchSaleOrderDetail(reqData).then((res) => {
        vm.loadingTable = false
        var data = res.data
        if (data.StatusCode == 200) {
          vm.tableData = data.Data.Items
          vm.total = data.Data.TotalCount
          vm.Extras = data.Extras
        } else {
          vm.$Notice.error({
            title: data.Errors
          })
        }
      })
    },
   
    ExportData () {
      let vm=this
      if (vm.tableData.length) {
        let index=1
         vm.tableData.forEach(element => {
          element.index=index++;
          
         });
        const params = {
          title: vm.tableTitle.map(x=>x.title),
          key: vm.tableTitle.map(x=>x.key),
          data: vm.tableData,
          autoWidth: true,
          filename: '销售明细',
        }; 
        excel.export_array_to_excel(params); 
      } else {
        this.$Message.info("导出数据不能为空！");
      }
    }
  },
  mounted () {
    this.tableHeight = window.innerHeight - 300
    this.sdate = this.dayjs().subtract(1, 'month').date(1).format('YYYY年MM月DD日')
    this.edate = this.dayjs().format('YYYY年MM月DD日')
    this.SearchSaleOrderDetail()
  }

}
</script>

<style>
</style>
